import useConfigStore from "@/stores/config";
import useUserStore from "@/stores/user";
import { isEmptyObject } from "@/utils/validate";
import Language from "@/utils/language";

export default defineNuxtRouteMiddleware(async (to, from) => {
    const configStore = useConfigStore();
    const userStore = useUserStore();
    try {
        // 获取配置信息
        if (isEmptyObject(configStore.sysConfig)) {
            await configStore.getConfig();
        }

        // 初始化插件语言
        const language = new Language(useNuxtApp().$i18n);
        if(to.meta?.addon) {
            language.loadAddonLocaleLang(String(to.meta?.addon), language.getLoadLocale().value);
        }

        // 判断是否关闭渠道
        if(configStore?.loginConfig?.config?.status == 0 && to.path !== "/close") {
            return navigateTo("/close");
        }

        // 防止获取用户信息异常
        if (userStore.isLogin && isEmptyObject(userStore.info)) {
            await userStore.getUser();
        }
    } catch (error) {
        userStore.$reset();
    }
});
